package com.txzt.gaea.contract.controller;

import com.txzt.base.constant.RtnConstant;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.page.PageInfoBT;
import com.txzt.gaea.contract.dto.BizContractDTO;
import com.txzt.gaea.contract.model.BizContract;
import com.txzt.gaea.contract.service.BizContractService;
import com.txzt.gaea.contract.vo.BizContractVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/biz/contract")
public class BizContractController extends BaseController {

    @Autowired
    private BizContractService bizContractService;

    /**
     * 档案统计
     * @return
     */
    @GetMapping(value = "/statistics")
    public RtnInfo<?> statistics() {
        return RtnInfo.success(bizContractService.statisticsBizContract());
    }

    @PostMapping(value = "/list")
    public RtnInfo<PageInfoBT<BizContractVo>> pageList(@RequestBody BizContractDTO bizContractDTO){
        return bizContractService.pageList(bizContractDTO);
    }

    @PostMapping(value = "/add")
    public RtnInfo add(@RequestBody BizContract bizContract){
        if (null != bizContract && null == bizContract.getId()){
            //查重
//            if (!bizContractService.addCheck(bizContract)){
//                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查合同编号、合同名称");
//            }
            int num = bizContractService.save(bizContract);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据添加失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/item/{id}")
    public RtnInfo getInfoById(@PathVariable Long id){
        return bizContractService.getInfo(id);
    }

    @PostMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo edit(@RequestBody BizContract bizContract){
        if (null != bizContract && null != bizContract.getId()){
            //查重
            if (!bizContractService.updateCheck(bizContract)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查合同编号、合同名称");
            }
            int num = bizContractService.update(bizContract);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据更新失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/delete/{id}")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo remove(@PathVariable Long id){
        return RtnInfo.success(bizContractService.delete(id));
    }

    @GetMapping(value = "/finish/{id}")
    public RtnInfo finish(@PathVariable Long id){
        return bizContractService.finishContract(id);
    }
}
